﻿using AutoMapper;
using B.S.BasicData.Domain.Entities;
using B.S.BasicData.Error;
using B.S.BasicData.Infrastructure.InProfaces;
using B.S.BasicData.Write.Api.Application.Command.ProductsCommand;
using MediatR;

namespace B.S.BasicData.Write.Api.Application.CommandHander.ProductsCommandHander
{
    public class ProductsCreateCommandHander : IRequestHandler<ProductsCreateCommand, ApiResult<object>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<Products> ProductsRepository;

        public ProductsCreateCommandHander(IMapper mapper, IBaseRepository<Products> productsRepository, IBaseRepository<Shop> shopRepository)
        {
            this.mapper = mapper;
            ProductsRepository = productsRepository;
        }

        public Task<ApiResult<object>> Handle(ProductsCreateCommand request, CancellationToken cancellationToken)
        {
            ApiResult<object> result = new ApiResult<object>();

            var list = mapper.Map<Products>(request);

            list.CreateName = "吕岳其";
            list.CreateTime = DateTime.Now;
            list.UpdateName = "";
            list.IsDel = false;

            var model = ProductsRepository.Add(list).Result;

            result.Code = ApiEnum.success;
            result.Msg = "添加成功";

            return Task.FromResult(result);
        }
    }
}
